﻿// ****************************************************************************'
//  프로그램 : 
//  설    명 : 
//  작성일자 : 2013-05-03
//  작성  자 : Jaeyong Park
//  특이사항 : 
//  수정내용 : 
// ****************************************************************************'

#region Using

using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

#endregion

namespace FormPrint
{
	public class PrintPreviewNormal
	{
		#region "1. 선언부"

		private PrintAction printAction = PrintAction.PrintToPreview;

		/// <summary>
		/// 출력할 이미지
		/// </summary>
		private Image _img = null;

		public PrintPreviewNormal()
		{
		}

		#endregion

		#region "2. 초기화"

		#endregion

		#region "3. 객체 제어"

		private void pd_BeginPrint(object sender, PrintEventArgs e)
		{
			printAction = e.PrintAction;
		}

		private void pd_PrintPage(object sender, PrintPageEventArgs e)
		{
			if(_img != null)
			{
				Size drawSize = StaticFn.GetPreferSize(_img.Size, e.MarginBounds.Size);

				// pd.OriginAtMargins 때문에 미리보기와 실제 출력시 위치 맞춤
				if(printAction == PrintAction.PrintToPreview)
				{
					e.Graphics.DrawImage(_img, new Rectangle(e.MarginBounds.Location, drawSize)
					                     , new Rectangle(Point.Empty, _img.Size), GraphicsUnit.Pixel);
				} else
				{
					e.Graphics.DrawImage(_img, new Rectangle(Point.Empty, drawSize)
					                     , new Rectangle(Point.Empty, _img.Size), GraphicsUnit.Pixel);
				}
			}
			e.HasMorePages = false;
		}

		#endregion

		#region "4. 사용자 정의 함수"

		public void Print(Form frm, bool preview)
		{
			_img = StaticFn.CaptureForm(frm);

			PrintDocument pd = new PrintDocument();

			// 프린트 기본 설정 - 추후 변경 가능
			pd.DefaultPageSettings.Margins = new Margins(20, 20, 20, 20);

			pd.BeginPrint += pd_BeginPrint;
			pd.PrintPage += pd_PrintPage;

			if(preview)
			{
				PrintPreviewDialog ppd = new PrintPreviewDialog();
				ppd.Document = pd;
				ppd.ShowDialog();
			} else
			{
				pd.Print();
			}
		}

		#endregion

		#region "5. 프로퍼티"

		#endregion
	}
}